/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DiariaSelector.java
 *
 * Created on Apr 30, 2009, 1:06:54 PM
 */

package doghost.selectors;

import javax.swing.RowFilter;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableRowSorter;
import org.jdesktop.application.Action;
import doghost.controllers.DiariaController;
import doghost.entities.Diaria;

/**
 *
 * @author duo
 */
public class DiariaSelector extends javax.swing.JDialog {

    protected DiariaController diariaController;
    protected Diaria selectedDiaria;

    /** Creates new form DiariaSelector */
    public DiariaSelector(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        diariaController = new DiariaController();
        diariaTable.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {


            @Override
                    public void valueChanged(ListSelectionEvent e) {
                        firePropertyChange("diariaRecordSelected",
                                !isDiariaRecordSelected(), isDiariaRecordSelected());
                    }
                });
    }

    public Diaria getSelectedDiaria() {
        return selectedDiaria;
    }

    public void setSelectedDiaria(Diaria selectedDiaria) {
        this.selectedDiaria = selectedDiaria;
    }

    public boolean isDiariaRecordSelected() {
        return diariaTable.getSelectedRow() != -1;
    }

    @Action
    public void newDiaria() {
        diariaController.newDiaria(doghostPUEntityManager, diariaList);
    }

    @Action(enabledProperty = "diariaRecordSelected")
    public void editDiaria() {
        int index = diariaTable.getSelectedRow();
        diariaController.setDiaria(diariaList.get(diariaTable.
                convertRowIndexToModel(index)));
        diariaController.editDiaria(doghostPUEntityManager, diariaList,
                diariaTable.convertRowIndexToModel(index));
    }

    @Action(enabledProperty = "diariaRecordSelected")
    public void deleteDiaria() {
        int index = diariaTable.getSelectedRow();
        diariaController.setDiaria(diariaList.get(diariaTable.
            convertRowIndexToModel(index)));
        diariaController.deleteDiaria(doghostPUEntityManager, diariaList,
            diariaTable.convertRowIndexToModel(index));
    }

    @Action
    public void cancelDiariaSelector() {
        setSelectedDiaria(null);
        setVisible(false);
    }

    @Action(enabledProperty = "diariaRecordSelected")
    public void selectDiaria() {
        int index = diariaTable.getSelectedRow();
        setSelectedDiaria(diariaList.get(diariaTable.
                convertRowIndexToModel(index)));
        setVisible(false);
    }

    @Action
    public void filterDiaria() {
        TableRowSorter diariaSorter = (TableRowSorter) diariaTable.getRowSorter();
        String text = diariaFilterTextField.getText();
        diariaFilterTextField.setText("");
        if(text.length()==0) {
            diariaSorter.setRowFilter(null);
        } else {
            diariaSorter.setRowFilter(RowFilter.regexFilter("(?u)(?i)" + text));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        customCurrencyRenderer1 = new doghost.utils.CustomCurrencyRenderer();
        customDateRenderer1 = new doghost.utils.CustomDateRenderer();
        doghostPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("doghostPU").createEntityManager();
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getResourceMap(DiariaSelector.class);
        diariaQuery = java.beans.Beans.isDesignTime() ? null : doghostPUEntityManager.createQuery(resourceMap.getString("diariaQuery.query")); // NOI18N
        diariaList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(diariaQuery.getResultList());
        diariaFilterTextField = new javax.swing.JTextField();
        diariaFilterButton = new javax.swing.JButton();
        newButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        diariaSelectionPanel = new org.jdesktop.swingx.JXPanel();
        diariaScrollPane = new javax.swing.JScrollPane();
        diariaTable = new javax.swing.JTable();

        customCurrencyRenderer1.setText(resourceMap.getString("customCurrencyRenderer1.text")); // NOI18N
        customCurrencyRenderer1.setName("customCurrencyRenderer1"); // NOI18N

        customDateRenderer1.setText(resourceMap.getString("customDateRenderer1.text")); // NOI18N
        customDateRenderer1.setName("customDateRenderer1"); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        diariaFilterTextField.setName("diariaFilterTextField"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getActionMap(DiariaSelector.class, this);
        diariaFilterButton.setAction(actionMap.get("filterDiaria")); // NOI18N
        diariaFilterButton.setName("diariaFilterButton"); // NOI18N

        newButton.setAction(actionMap.get("newDiaria")); // NOI18N
        newButton.setName("newButton"); // NOI18N

        editButton.setAction(actionMap.get("editDiaria")); // NOI18N
        editButton.setName("editButton"); // NOI18N

        deleteButton.setAction(actionMap.get("deleteDiaria")); // NOI18N
        deleteButton.setName("deleteButton"); // NOI18N

        cancelButton.setAction(actionMap.get("cancelDiariaSelector")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N

        diariaSelectionPanel.setName("diariaSelectionPanel"); // NOI18N

        diariaScrollPane.setName("diariaScrollPane"); // NOI18N

        diariaTable.setAutoCreateRowSorter(true);
        diariaTable.setName("diariaTable"); // NOI18N

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, diariaList, diariaTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${inicio}"));
        columnBinding.setColumnName("Inicio");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${valor}"));
        columnBinding.setColumnName("Valor");
        columnBinding.setColumnClass(Double.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        diariaScrollPane.setViewportView(diariaTable);
        diariaTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("diariaTable.columnModel.title0")); // NOI18N
        diariaTable.getColumnModel().getColumn(0).setCellRenderer(customDateRenderer1);
        diariaTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("diariaTable.columnModel.title1")); // NOI18N
        diariaTable.getColumnModel().getColumn(1).setCellRenderer(customCurrencyRenderer1);

        javax.swing.GroupLayout diariaSelectionPanelLayout = new javax.swing.GroupLayout(diariaSelectionPanel);
        diariaSelectionPanel.setLayout(diariaSelectionPanelLayout);
        diariaSelectionPanelLayout.setHorizontalGroup(
            diariaSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(diariaSelectionPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(diariaScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 358, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        diariaSelectionPanelLayout.setVerticalGroup(
            diariaSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(diariaScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 413, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(diariaFilterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(diariaFilterButton))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(editButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(deleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancelButton))
                    .addComponent(diariaSelectionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 394, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(diariaFilterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(diariaFilterButton))
                .addGap(18, 18, 18)
                .addComponent(diariaSelectionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(newButton)
                    .addComponent(editButton)
                    .addComponent(deleteButton)
                    .addComponent(cancelButton))
                .addContainerGap(43, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DiariaSelector dialog = new DiariaSelector(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private doghost.utils.CustomCurrencyRenderer customCurrencyRenderer1;
    private doghost.utils.CustomDateRenderer customDateRenderer1;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton diariaFilterButton;
    private javax.swing.JTextField diariaFilterTextField;
    private java.util.List<doghost.entities.Diaria> diariaList;
    private javax.persistence.Query diariaQuery;
    private javax.swing.JScrollPane diariaScrollPane;
    private org.jdesktop.swingx.JXPanel diariaSelectionPanel;
    private javax.swing.JTable diariaTable;
    private javax.persistence.EntityManager doghostPUEntityManager;
    private javax.swing.JButton editButton;
    private javax.swing.JButton newButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
